﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QuanLyDoiXe.lop_doi_tuong;
using QuanLyDoiXe.Lop_du_lieu;
using System.IO;
using BarcodeLib;


namespace QuanLyDoiXe
{
    public partial class frmtiepnhanxe : Form
    {
        SqlConnection conn = new SqlConnection("Data Source=ACER;Initial Catalog=quanlydoixevinamilk;Integrated Security=True");
        public frmtiepnhanxe()
        {
            InitializeComponent();
        }
        #region 1. Hien thi xe
        ListViewItem item;
        private void HienThiXe()
        {
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("Select * from Xe,loaihang,hangxe,trongtai,NVnhanxe where Xe.Mloai=loaihang.Mloai and Xe.Mhang=hangxe.Mhang and Xe.MTT=trongtai.MTT and Xe.MNV=NVnhanxe.MNV ", conn);
                SqlDataReader sqlDA = cmd.ExecuteReader();
                int i = 0;
                lvxechotiepnhan.View = View.Details;
                lvxechotiepnhan.FullRowSelect= true;
                while (sqlDA.Read())
                {
                    i++;
                    item = new ListViewItem(i.ToString());
                    item.SubItems.Add(sqlDA["Mxe"].ToString());
                    item.SubItems.Add(sqlDA["Bienso"].ToString());
                    item.SubItems.Add(sqlDA["Tenhang"].ToString());
                    item.SubItems.Add(sqlDA["Loaihang"].ToString());
                    item.SubItems.Add(sqlDA["Trongtai"].ToString());
                    item.SubItems.Add(sqlDA["Sokhung"].ToString());
                    item.SubItems.Add(sqlDA["Somay"].ToString());
                   // item.SubItems.Add(sqlDA["Hieu"]
                    item.SubItems.Add(sqlDA["NamSx"].ToString());
                    item.SubItems.Add(sqlDA["TenNV"].ToString());
                    item.SubItems.Add(sqlDA["Ngaydangkiem"].ToString());
                    item.SubItems.Add(sqlDA["Ngaytiepnhan"].ToString());
                    lvxechotiepnhan.Items.Add(item);
                }
                cmd.Dispose();
                sqlDA.Close();
                conn.Close();
            }
            catch
            {
            }
        }

        private void lvxechotiepnhan_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            ListViewItem item = e.Item;
            txtmaxe.Text = item.SubItems[1].Text;
            txtbienxo.Text = item.SubItems[2].Text;
            cbhangxe.Text = item.SubItems[3].Text;
            cbloaihang.Text = item.SubItems[4].Text;
            cbtrongtai.Text = item.SubItems[5].Text;
            txtsokhung.Text = item.SubItems[6].Text;
            txtsomay.Text = item.SubItems[7].Text;
          //  txthieuxe.Text = item.SubItems[8].Text;
            txtnamsx.Text = item.SubItems[8].Text;
            txtnhanviennhanxe.Text = item.SubItems[9].Text;
            dtpdangkiem.Text = item.SubItems[10].Text;
            dtpngaytiepnhan.Text = item.SubItems[11].Text;
        }
        string Mxe, Bienso, Hangxe, Loaihang, Sokhung, Somay, TenNV;
        int Trongtai, NamSx;
        DateTime Ngaydangkiem;
        DateTime Ngaytiepnhan;
        private void lvxechotiepnhan_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < lvxechotiepnhan.Items.Count; i++)
            {
                if (lvxechotiepnhan.Items[i].Selected)
                {
                    Mxe = item.SubItems[1].Text;
                    Bienso = item.SubItems[2].Text;
                    Hangxe = item.SubItems[3].Text;
                    Loaihang=item.SubItems[4].Text;
                    Trongtai = Convert.ToInt32(item.SubItems[5].Text);
                    Sokhung = item.SubItems[6].Text;
                    Somay = item.SubItems[7].Text;
                    //Hieuxe = item.SubItems[8].Text;
                    NamSx = Convert.ToInt32(item.SubItems[8].Text);
                    TenNV = item.SubItems[9].Text;
                    Ngaydangkiem = Convert.ToDateTime(item.SubItems[10].Text);
                    Ngaytiepnhan = Convert.ToDateTime(item.SubItems[11].Text);
                }
            }
        }

        private void frmtiepnhanxe_Load(object sender, EventArgs e)
        {
            HienThiXe();
        }
        #endregion
        #region 2. thoat & nhap lai
        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnnhaplai_Click(object sender, EventArgs e)
        {
            txtbienxo.Text = "";
            txtnhanviennhanxe.Text = "";
            txtsokhung.Text = "";
            txtsomay.Text = "";
            dtpdangkiem.ResetText();
            dtpngaytiepnhan.ResetText();
            txtnamsx.Text = "";
            cbhangxe.ResetText();
            cbloaihang.ResetText();
            cbtrongtai.ResetText();
        }
        #endregion
        #region 3. them
        private void btnthem_Click(object sender, EventArgs e)
        {
            string strMaTT = "";
            Lop_lay_du_lieu dl = new Lop_lay_du_lieu();
            SqlDataReader dr = dl.lay_reader("select MTT from trongtai where Trongtai=N\'" + cbtrongtai.Text + "\'");
            while (dr.Read())
            {
                strMaTT = dr.GetString(0);
            }
            dr.Close(); dr.Dispose();

            string strNVnhanxe = "";
            Lop_lay_du_lieu dl1 = new Lop_lay_du_lieu();
            SqlDataReader dr1 = dl1.lay_reader("select MNV from NVnhanxe where TenNV=N\'" + txtnhanviennhanxe.Text + "\'");
            while (dr1.Read())
            {
                strNVnhanxe = dr1.GetString(0);
            }
            dr1.Close(); dr1.Dispose();
            string strMaloai = "";
            Lop_lay_du_lieu dl2 = new Lop_lay_du_lieu();
            SqlDataReader dr2 = dl2.lay_reader("select Mloai from loaihang where Loaihang=N\'" + cbloaihang.Text + "\'");
            while (dr2.Read())
            {
                strMaloai = dr2.GetString(0);
            }
            dr2.Close(); dr2.Dispose();
            string strMahang = "";
            Lop_lay_du_lieu dl3 = new Lop_lay_du_lieu();
            SqlDataReader dr3 = dl3.lay_reader("select Mhang from hangxe where tenhang=N\'" + cbhangxe.Text + "\'");
            while (dr3.Read())
            {
                strMahang = dr3.GetString(0);
            }
            dr3.Close(); dr3.Dispose();
            lop_ket_noi.huyketnoi();

            txtmaxe.Enabled = true;
            if (txtmaxe.Text != "" && txtbienxo.Text != "" && cbhangxe.Text != "" && cbtrongtai.Text != "" && cbloaihang.Text != "" && txtnhanviennhanxe.Text != "" && txtsokhung.Text != "" && txtsomay.Text != "" && txtnamsx.Text != "" && dtpdangkiem.Text != "" && dtpngaytiepnhan.Text != "")
            {
                tiepnhanxe Xe = new tiepnhanxe(txtmaxe.Text, txtbienxo.Text, strMahang, strMaTT, strNVnhanxe, strMaloai, txtsokhung.Text, txtsomay.Text, Convert.ToInt32(txtnamsx.Text), Convert.ToDateTime(dtpdangkiem.Text), Convert.ToDateTime(dtpngaytiepnhan.Text));
                if (Xe.taomoi())
                    MessageBox.Show("Quá trình tạo mới xe thành công!");
                else
                    MessageBox.Show("Quá trình tạo mới xe thất bại!");
            }
            else
                MessageBox.Show("Bạn hãy nhập đầy đủ hông tin xe! ");
            lvxechotiepnhan.Items.Clear();
            HienThiXe();
        }
        #endregion
        #region 4. xoá
        private void btnxoa_Click(object sender, EventArgs e)
        {
            lvxechotiepnhan.Items.Remove(lvxechotiepnhan.FocusedItem);
            Lop_lay_du_lieu dl = new Lop_lay_du_lieu();
            SqlDataReader dr = dl.lay_reader("select Ngaytiepnhan from Xe where Mxe='" + txtmaxe.Text + "'");
            while (dr.Read())
            {
                Ngaytiepnhan = Convert.ToDateTime(dr["Ngaytiepnhan"].ToString());
            }
            lop_ket_noi.huyketnoi();
            Lop_lay_du_lieu dl1 = new Lop_lay_du_lieu();
            SqlDataReader dr1 = dl.lay_reader("select Ngaydangkiem from Xe where Mxe='" + txtmaxe.Text + "'");
            while (dr1.Read())
            {
                Ngaydangkiem = Convert.ToDateTime(dr1["Ngaydangkiem"].ToString());
            }
            lop_ket_noi.huyketnoi();

            string strMahang = "";
            Lop_lay_du_lieu dl2 = new Lop_lay_du_lieu();
            SqlDataReader dr2= dl2.lay_reader("select Mhang from hangxe where tenhang=N\'" + cbhangxe.Text + "\'");
            while (dr2.Read())
            {
                strMahang = dr2.GetString(0);
            }
            dr2.Close(); dr2.Dispose();
            string strMaTT = "";
            Lop_lay_du_lieu dl3 = new Lop_lay_du_lieu();
            SqlDataReader dr3 = dl3.lay_reader("select MTT from trongtai where Trongtai=N\'" + cbtrongtai.Text + "\'");
            while (dr3.Read())
            {
                strMaTT = dr3.GetString(0);
            }
            dr3.Close(); dr3.Dispose();

            string strNVnhanxe = "";
            Lop_lay_du_lieu dl4 = new Lop_lay_du_lieu();
            SqlDataReader dr4 = dl4.lay_reader("select MNV from NVnhanxe where TenNV=N\'" + txtnhanviennhanxe.Text + "\'");
            while (dr4.Read())
            {
                strNVnhanxe = dr4.GetString(0);
            }
            dr4.Close(); dr4.Dispose();
            string strMaloai = "";
            Lop_lay_du_lieu dl5 = new Lop_lay_du_lieu();
            SqlDataReader dr5 = dl5.lay_reader("select Mloai from loaihang where Loaihang=N\'" + cbloaihang.Text + "\'");
            while (dr5.Read())
            {
                strMaloai = dr5.GetString(0);
            }
            dr5.Close(); dr5.Dispose();
            tiepnhanxe Xe = new tiepnhanxe(Mxe,Bienso,strMahang,strMaTT,strNVnhanxe,strMaloai,Sokhung,Somay,NamSx,Ngaydangkiem,Ngaytiepnhan);
            if (Xe.xoa())
            {
                MessageBox.Show("Quá trình xóa Xe ra khỏi danh sách thành công !");
                txtmaxe.Text="";
                txtbienxo.Text="";
                txtnhanviennhanxe.Text="";
                txtsokhung.Text="";
                txtsomay.Text="";
                cbhangxe.Text="";
                cbloaihang.Text="";
                cbtrongtai.Text="";
                txtnamsx.Text = "";
                dtpdangkiem.Text = "";
                dtpngaytiepnhan.Text = "";
            }

            else
                MessageBox.Show("Quá trinh xóa đia điểm ra khỏi danh thất bại !");
            lvxechotiepnhan.Items.Clear();
            HienThiXe();

        }
        #endregion
        #region 5. cap nhap
        private void btncapnhap_Click(object sender, EventArgs e)
        {
            string strMaTT = "";
            Lop_lay_du_lieu dl = new Lop_lay_du_lieu();
            SqlDataReader dr = dl.lay_reader("select MTT from trongtai where Trongtai=N\'" + cbtrongtai.Text + "\'");
            while (dr.Read())
            {
                strMaTT = dr.GetString(0);
            }
            dr.Close(); dr.Dispose();

            string strNVnhanxe = "";
            Lop_lay_du_lieu dl1 = new Lop_lay_du_lieu();
            SqlDataReader dr1 = dl1.lay_reader("select MNV from NVnhanxe where TenNV=N\'" + txtnhanviennhanxe.Text + "\'");
            while (dr1.Read())
            {
                strNVnhanxe = dr1.GetString(0);
            }
            dr1.Close(); dr1.Dispose();
            string strMaloai = "";
            Lop_lay_du_lieu dl2 = new Lop_lay_du_lieu();
            SqlDataReader dr2 = dl2.lay_reader("select Mloai from loaihang where Loaihang=N\'" + cbloaihang.Text + "\'");
            while (dr2.Read())
            {
                strMaloai = dr2.GetString(0);
            }
            dr2.Close(); dr2.Dispose();
            string strMahang = "";
            Lop_lay_du_lieu dl3 = new Lop_lay_du_lieu();
            SqlDataReader dr3 = dl3.lay_reader("select Mhang from hangxe where tenhang=N\'" + cbhangxe.Text + "\'");
            while (dr3.Read())
            {
                strMahang = dr3.GetString(0);
            }
            dr3.Close(); dr3.Dispose();
            lop_ket_noi.huyketnoi();
            if (txtmaxe.Text != "" && txtbienxo.Text != "" && cbhangxe.Text != "" && cbtrongtai.Text != "" && cbloaihang.Text != "" && txtnhanviennhanxe.Text != "" && txtsokhung.Text != "" && txtsomay.Text != "" && txtnamsx.Text != "" && dtpdangkiem.Text != "" && dtpngaytiepnhan.Text != "")
            {
                tiepnhanxe Xe = new tiepnhanxe(txtmaxe.Text,txtbienxo.Text,strMahang,strMaTT,strNVnhanxe,strMaloai,txtsokhung.Text,txtsomay.Text,Convert.ToInt32(txtnamsx.Text),Convert.ToDateTime(dtpdangkiem.Text),Convert.ToDateTime(dtpngaytiepnhan.Text));
                if (Xe.capnhat())
                    MessageBox.Show("Quá trình cập nhật xe thành công!");
                else
                    MessageBox.Show("Quá trình cập nhật xe thất bại!");
            }
            else
                MessageBox.Show("Bạn hãy nhập đầy đủ thông tin xe! ");
            lvxechotiepnhan.Items.Clear();
            HienThiXe();
        }
        #endregion
        #region 6. Khởi tạo
        string Maxe(string ma)
        {
            Lop_lay_du_lieu dl1 = new Lop_lay_du_lieu();
            string tam = "";
            SqlDataReader dr1 = dl1.lay_reader("select Mxe from Xe");
            while (dr1.Read())
                tam = dr1[0].ToString();
            lop_ket_noi.huyketnoi();
            int i = 0;
            i = int.Parse(tam.Substring(2, tam.Length - 2));
            i++;
            if (i < 10) return "MX00" + i.ToString();
            else
                if (i < 100) return "MX0" + i.ToString();
                else return "MX" + i.ToString();
        }
        private void btnkhoitao_Click(object sender, EventArgs e)
        {
            Lop_lay_du_lieu dl = new Lop_lay_du_lieu();
            SqlDataReader dr = dl.lay_reader("select Mxe from Xe");
            string strtam = "";
            while (dr.Read())
                strtam = dr[0].ToString();
            lop_ket_noi.huyketnoi();
            if (strtam == "") txtmaxe.Text = "00000";
            else txtmaxe.Text = Maxe(strtam);
        }
        #endregion
    }
}
